import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);
// 解决Vuex刷新页面数据丢失的问题
// 利用第三方库进行持久化存储  使用vuex-persistedstate存储到sessionStorage
import createPersistedState from "vuex-persistedstate"
export default new Vuex.Store({
    state: {
        userInfo:{},
        openTab: [
            {
                name: 'analysis',
                title: '首页',
                closable: false
            }
        ],
        activeIndex: ''
    },
    getters: {
    },
    mutations: {
        change_userInfo(state,data){
            state.userInfo = data;
        },
        add_tabs(state, data) {
            if (state.openTab.some(item => item.title === data.title)) return
            state.openTab.push(data);
        },
        delete_tabs(state, route) {
            let index = 0;
            for (let i of state.openTab) {
                if (i.route === route) {
                    break;
                }
                index++;
            }
            state.openTab.splice(index, 1);
        },
        set_active_tabs(state, index) {
            state.activeIndex = index;
        }
    },
    actions: {},
    modules: {},
    plugins: [createPersistedState({
        storage:window.sessionStorage  // 同localStorage相同，只是将vuex的所有值存储到sessionStorage中
    })]
});
